package com.jumi.microservice.controller;

import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.domain.app.AppVersion;
import com.jumi.microservice.jwt.annon.IgnoreUrlsAnnon;
import com.jumi.microservice.service.AppVersionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ssk
 * @create 20/10/29 10:49
 **/
@RestController
@RequestMapping
@Validated
@Api(tags = "app版本控制接口")
public class AppVersionController extends BaseController {


    @Resource
    private AppVersionService appVersionService;

    @PostMapping("/appVersion/add")
    @ApiOperation("新增接口")
    public ResponseResult add(AppVersion appVersion, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        appVersion.setCreateBy(createBy);
        int rows = appVersionService.save(appVersion);
        return ResponseResult.success("操作成功", rows);
    }

    @GetMapping("/appVersion/findByPage")
    @ApiOperation("分页查询接口")
    public TableDataInfo<List<AppVersion>> findByPage(AppVersion appVersion) {
        startPage();
        List<AppVersion> list = appVersionService.findAll(appVersion);
        return getDataTable(list);
    }


    @GetMapping("/api/appVersion/findLatestVersionNo")
    @IgnoreUrlsAnnon("/api/appVersion/findLatestVersionNo")
    @ApiOperation("根据平台渠道获取最新版本号")
    @ApiImplicitParam(name = "loginClient",value = "平台渠道",required = true)
    public ResponseResult findLatestVersionNo(String loginClient) {
        Integer platform;
        switch (loginClient){
            case "android":
                platform=0;
                break;
            case "ios":
                platform=1;
                break;
            case "web":
                platform=2;
                break;
            default:
                throw new BaseException(500, "请传输数据平台类型:loginClient");
        }
        AppVersion version = appVersionService.findLatestVersion();
        if(platform==2){
            return ResponseResult.success("操作成功", version);
        }
        Map<String,Object> map=new HashMap<>();
        if(platform==0){
            map.put("androidNo",version.getAndroidNo());
            map.put("h5AndroidNo",version.getH5AndroidNo());
        }else{
            map.put("iosNo",version.getIosNo());
            map.put("h5IosNo",version.getH5IosNo());
        }
        map.put("isForced",version.getIsForced());
        map.put("description",version.getDescription());
        return ResponseResult.success("操作成功", map);
    }

}
